import { apiCoptcSelectCoptcList } from '@/api/url/salesOrderManagement';
import router from '@/router';
import { isNullPm, isParam } from '@/utils/baseMethods';
import { reactive, watch } from 'vue';
import { dayjs } from '@arco-design/web-vue/es/_utils/date';

export default function importConfig() {
  const base = reactive<any>({
    pagination: {
      total: 100, //总条数
      current: 1,
      pageSize: 10,
    },
    tabelList: null,
    checkFrom: {
      customName: '',
      deliveryDate: '',
      orderDate: '',
      orderId: '',
      orderType: '',
      userId: '',
    },
    rowSelection: {
      type: 'checkbox',
      showCheckedAll: true,
      onlyCurrent: false,
    },
    expandable: {
      title: '详情',
      width: 80,
      expandedRowRender: (record): any => {
        return record.tc001 + record.tc002;
      },
    },
    // config:['derive']
  });

  const init = () => {
    getTable();
  };

  /**
   * @exports getTable 销售订单列表数据获取
   */
  const getTable = () => {
    const steam = {};
    const relt = JSON.parse(JSON.stringify(base.checkFrom));
    if (relt.orderDate) {
      relt.orderDate = dayjs(relt.orderDate).format('YYYYMMDD');
    }
    if (relt.deliveryDate) {
      relt.deliveryDate = dayjs(relt.deliveryDate).format('YYYYMMDD');
    }

    Object.keys(relt).map((res: any) => {
      if (!isNullPm(relt[res])) {
        steam[res] = relt[res];
      }
    });

    const req = {
      pageSize: base.pagination.pageSize,
      current: base.pagination.current,
    };

    apiCoptcSelectCoptcList(req, steam).then((res: any) => {
      if (res.code == '200') {
        base.tabelList = res.data.records;
        base.tabelList.map((rev: any, index: any) => {
          rev.key = index;
        });
        base.pagination.total = res.data.total;
      }
    });
  };

  /**
   * @exports skipSalesDetails 跳转方法 跳转到 salesDetails 页面
   */
  const skipSalesDetails = (param: any) => {
    router.push({ path: 'salesDetails', query: { UUID: param.uuid } });
  };

  /**
   * @exports addSalesDetails 跳转到新增销售订单页面
   */
  const addSalesDetails = () => {
    router.push('salesDetails');
  };

  /**
   * @exports skipDetails 查看详情跳转
   */
  const skipDetails = (param) => {
    router.push({ path: '/viewSalesDetails', query: { db: param.tc001, dh: param.tc002 } });
  };

  //重置
  const reset = () => {
    base.checkFrom = {
      customName: '',
      deliveryDate: '',
      orderDate: '',
      orderId: '',
      orderType: '',
      userId: '',
    };
    init();
  };

  watch(
    () => base.pagination,
    () => {
      getTable();
    },
    {
      deep: true,
    },
  );

  return {
    base,
    init,
    skipSalesDetails,
    addSalesDetails,
    reset,
    skipDetails,
  };
}
